import { Component, OnInit } from '@angular/core';
import { StorageService } from '../../services/storage.service';
import { NativeService } from '../../services/native.service';
import { StorageKey } from '../../Constants';
import { UserInfo } from '../../models/UserInfo';
import {Router} from '@angular/router';

@Component({
	selector: 'app-home',
	templateUrl: './home.page.html',
	styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

	private showPopover = false;
    private isFirst;
    private name;
    private phoneNumber;

	constructor(
		private storage: StorageService,
		private nativeService: NativeService,
		private router: Router
	) { }

	ngOnInit() {
		
	}
	
	async presentPopover() {
        this.showPopover = !this.showPopover;
    }
    async onPageDidEnter() {
        this.showPopover = false;
        let userInfo: UserInfo = await this.storage.read(StorageKey.PERSON_INFO);
        userInfo = userInfo ? userInfo : {};
        if (userInfo) {
            this.isFirst =  userInfo.isFirst;
            this.name = userInfo.userName;
            this.phoneNumber = userInfo.phoneNumber;
        }
        if (this.isFirst == '0') {
            console.log('首次登陆');
            this.nativeService.showAlert('为了您账户的安全，请修改密码', '前往修改密码', () => {
                this.router.navigate(['/change-password'], {
                    queryParams: {
                        url: '/home',
                    }
                })
            })
        } else {
            console.log('非首次登录')
        }
    }

    onPageDidLeave() {
        this.showPopover = false;
    }

    to(url) {
        this.router.navigate([url])
    }
}
